public with sharing class MobileAppRequestExtension
{
    public Account currentAccount {get;set;}
    public Request__c currentRecord {get;set;}
    public Transaction_Types__c currentTransactionType{get;set;}
    public Location__c newLocation {get;set;}
    public Requested_For__c currentRequestedFor {get;set;}
    public Country__c currentVisitingCountry {get;set;}
    public Country__c currentCourierCountry{get;set;}
    public Country__c currentOldDrivingLicenseCountry {get;set;}
    public Country__c currentNewDrivingLicenseCountry {get;set;}
    public Requested_For__c currentPurposeOfVisit {get;set;}
    public Contact currentEmployee {get;set;}
    public List<Contact> entryPermitsEmployeesList {get;set;}
    public Requested_For__c currentPort {get;set;}
    public Requested_For__c currentRequestType {get;set;}
    public Requested_For__c currentOldDrivingLicenseIssued {get;set;}
    public Requested_For__c currentNewDrivingLicenseIssued {get;set;}
    public Requested_For__c currentEmirate {get;set;}
    public Requested_For__c currentVehicleType {get;set;}
    public Requested_For__c currentAddressTo {get;set;}
    public Occupation__c previousJobTitle {get;set;}
    //Added by Mustafa Zaidi [17th April 2014]
    Map<string, ID> recordTypeMap = new Map<string, ID>();
    Map<ID, Requested_For__c> requestedForMap = new Map<ID, Requested_For__c>();
    Map<ID, Country__c> countryMap = new Map<ID, Country__c>();
    Map<ID, Contact> contactMap = new Map<ID, Contact>();
    
    string recordTypeName = null;
    
    public List<requestProductInformation> requestProductInformationList {get; set;}
    
    public class requestProductInformation 
    {
        public String InvoiceNumber {get; set;}
        public String ShipmentNumber {get; set;}
        public Date productDate {get; set;}
        public Country__c countryObject {get; set;}
        public Requested_For__c requestedForTypeObject {get; set;}
        
        public requestProductInformation()
        {}
    }
     
    public MobileAppRequestExtension()
    {
        
    }
    
    public MobileAppRequestExtension(ApexPages.StandardController controller) 
    {
        
        //Initializing variables...
        currentAccount = new Account();
        currentRecord  = new Request__c();
        currentTransactionType = new Transaction_Types__c();
        newLocation = new Location__c();
        currentRequestedFor = new Requested_For__c();
        currentVisitingCountry = new Country__c();
        currentCourierCountry = new Country__c();
        currentOldDrivingLicenseCountry = new Country__c();
        currentNewDrivingLicenseCountry = new Country__c();
        currentEmployee = new Contact();
        entryPermitsEmployeesList = new List<Contact>();
        currentPurposeOfVisit = new Requested_For__c();
        currentPort = new Requested_For__c();
        currentRequestType = new Requested_For__c();
        currentOldDrivingLicenseIssued = new Requested_For__c();
        currentNewDrivingLicenseIssued = new Requested_For__c();
        currentAddressTo = new Requested_For__c();
        currentEmirate = new Requested_For__c();
        currentVehicleType = new Requested_For__c();
        previousJobTitle = new Occupation__c();
        requestProductInformationList = new List<requestProductInformation>();
        currentRecord = (Request__c)controller.getRecord();
        
        if(ApexPages.currentPage().getParameters().get('numberOfProducts') != null)
        {
            for(Integer i = 0; i < Integer.valueOf(ApexPages.currentPage().getParameters().get('numberOfProducts')); i++) {
                requestProductInformationList.add(new requestProductInformation());
            }
        }
    
        if(ApexPages.currentPage().getParameters().get('requestedFor') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('requestedFor'), null);
        
        if(ApexPages.currentPage().getParameters().get('purposeVisit') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('purposeVisit'), null);
        
        if(ApexPages.currentPage().getParameters().get('portName') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('portName'), null);
        
        if(ApexPages.currentPage().getParameters().get('requestType') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('requestType'), null);    
        
        if(ApexPages.currentPage().getParameters().get('oldLicense') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('oldLicense'), null);
        
        if(ApexPages.currentPage().getParameters().get('newLicense') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('newLicense'), null);
        
        if(ApexPages.currentPage().getParameters().get('vehicleTypeId') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('vehicleTypeId'), null);
        
        if(ApexPages.currentPage().getParameters().get('emirateId') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('emirateId'), null);
        
        if(ApexPages.currentPage().getParameters().get('addressToId') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('addressToId'), null);
        
        if(ApexPages.currentPage().getParameters().get('productOne') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('productOne'), null);
        
        if(ApexPages.currentPage().getParameters().get('productTwo') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('productTwo'), null);
        
        if(ApexPages.currentPage().getParameters().get('productThree') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('productThree'), null);
        
        if(ApexPages.currentPage().getParameters().get('productFour') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('productFour'), null);
        
        if(ApexPages.currentPage().getParameters().get('productFive') != null)
            requestedForMap.put(ApexPages.currentPage().getParameters().get('productFive'), null);
        
        if(ApexPages.currentPage().getParameters().get('countryId') != null)
            countryMap.put(ApexPages.currentPage().getParameters().get('countryId'), null);
        if(ApexPages.currentPage().getParameters().get('oldCountry') != null)
            countryMap.put(ApexPages.currentPage().getParameters().get('oldCountry'), null);
        if(ApexPages.currentPage().getParameters().get('newCountry') != null)
            countryMap.put(ApexPages.currentPage().getParameters().get('newCountry'), null);
        if(ApexPages.currentPage().getParameters().get('visitingCountry') != null)
            countryMap.put(ApexPages.currentPage().getParameters().get('visitingCountry'), null);
        if(ApexPages.currentPage().getParameters().get('productOneCountry') != null)
            countryMap.put(ApexPages.currentPage().getParameters().get('productOneCountry'), null);
        if(ApexPages.currentPage().getParameters().get('productTwoCountry') != null)
            countryMap.put(ApexPages.currentPage().getParameters().get('productTwoCountry'), null);
        if(ApexPages.currentPage().getParameters().get('productThreeCountry') != null)
            countryMap.put(ApexPages.currentPage().getParameters().get('productThreeCountry'), null);
        if(ApexPages.currentPage().getParameters().get('productFourCountry') != null)
            countryMap.put(ApexPages.currentPage().getParameters().get('productFourCountry'), null);
        if(ApexPages.currentPage().getParameters().get('productFiveCountry') != null)
            countryMap.put(ApexPages.currentPage().getParameters().get('productFiveCountry'), null);
        
        if(ApexPages.currentPage().getParameters().get('transactionTypeId') != null)
        {
            for(Transaction_Types__c transactionTypeObject : [SELECT Id, Name, Application_Package__c, Application_Type__c, Transaction_Description__c FROM Transaction_Types__c WHERE Id = : ApexPages.currentPage().getParameters().get('transactionTypeId') LIMIT 1])
            {
                currentTransactionType = transactionTypeObject;
                
                
                String Application_Package = currentTransactionType.Application_Package__c.replaceAll( '\\s+', '');
                String Application_Type = currentTransactionType.Application_Type__c.replaceAll( '\\s+', '_');
                recordTypeName = 'Request_' + Application_Package + '_' + Application_Type;
                
            }
        }   

        if(ApexPages.currentPage().getParameters().get('contact') != null)
            contactMap.put(ApexPages.currentPage().getParameters().get('contact'), null);
        if(ApexPages.currentPage().getParameters().get('permitEmployeeOne') != null)
            contactMap.put(ApexPages.currentPage().getParameters().get('permitEmployeeOne'), null);
        if(ApexPages.currentPage().getParameters().get('permitEmployeeTwo') != null)
            contactMap.put(ApexPages.currentPage().getParameters().get('permitEmployeeTwo'), null);
        if(ApexPages.currentPage().getParameters().get('permitEmployeeThree') != null)
            contactMap.put(ApexPages.currentPage().getParameters().get('permitEmployeeThree'), null);
        if(ApexPages.currentPage().getParameters().get('permitEmployeeFour') != null)
            contactMap.put(ApexPages.currentPage().getParameters().get('permitEmployeeFour'), null);
        if(ApexPages.currentPage().getParameters().get('permitEmployeeFive') != null)
            contactMap.put(ApexPages.currentPage().getParameters().get('permitEmployeeFive'), null);
        
        if(ApexPages.currentPage().getParameters().get('previousJobId') != null)
        {    
            
            for(Occupation__c ocpObject : [SELECT Id, Name FROM Occupation__c WHERE Id =: ApexPages.currentPage().getParameters().get('previousJobId') LIMIT 1])
            {
                previousJobTitle = ocpObject;
            }
        }
        
        if(ApexPages.currentPage().getParameters().get('newLocation') != null)
        {    
            for(Location__c locationObject : [SELECT Id, Name FROM Location__c WHERE Id = :ApexPages.currentPage().getParameters().get('newLocation') LIMIT 1])
            {
                newLocation = locationObject;
            }
        }
           
        List<Contact> contactList = new List<Contact> 
                                            ([SELECT Id, Name, Current_Employer__c 
                                             FROM Contact 
                                             WHERE Id IN (Select ContactId FROM User WHERE Id = :UserInfo.getUserId())
                                             LIMIT 1]);
        
        Contact currentContact = null;
        
        if (contactList.size() > 0)
            currentContact = contactList[0];
        else
            currentContact = new Contact();
        
        List<Account> accountsList = new List<Account>([SELECT Id, Name, LicenseNumber__r.Name, Company_Name_Arabic__c,
                        Authorized_Manager__r.Name, Authorized_Manager__r.Arabic_Name__c,
                        Authorize_Manager_Nationality__c, Authorize_Manager_Passport_Number__c, Company_Capital__c,
                        License_State_Date__c, Activity__c
                        FROM Account WHERE Id = :currentContact.Current_Employer__c LIMIT 1]);
        
        if(accountsList.size() > 0)
            currentAccount = accountsList[0];
        
        for(RecordType recordTypeObject : [SELECT ID,DeveloperName FROM RecordType where SObjectType = 'Request__c' AND IsActive = true])
        {
            recordTypeMap.put(recordTypeObject.DeveloperName,recordTypeObject.Id);
        }
        
        //Now query to all the objects...
        if(countryMap.size() > 0)
        {
            for(Country__c countryObject : [SELECT Id, Name FROM Country__c WHERE Id IN: countryMap.keyset()])
            {
                if(ApexPages.currentPage().getParameters().get('countryId') == countryObject.Id)
                    currentCourierCountry = countryObject;    
                if(ApexPages.currentPage().getParameters().get('oldCountry') == countryObject.Id)
                    currentOldDrivingLicenseCountry = countryObject;
                if(ApexPages.currentPage().getParameters().get('newCountry') == countryObject.Id)
                    currentNewDrivingLicenseCountry = countryObject;    
                if(ApexPages.currentPage().getParameters().get('visitingCountry') == countryObject.Id)
                    currentVisitingCountry = countryObject; 
                if(ApexPages.currentPage().getParameters().get('productOneCountry') == countryObject.Id)
                    requestProductInformationList.get(0).countryObject = countryObject; 
                if(ApexPages.currentPage().getParameters().get('productTwoCountry') == countryObject.Id)
                    requestProductInformationList.get(1).countryObject = countryObject; 
                if(ApexPages.currentPage().getParameters().get('productThreeCountry') == countryObject.Id)
                    requestProductInformationList.get(2).countryObject = countryObject; 
                if(ApexPages.currentPage().getParameters().get('productFourCountry') == countryObject.Id)
                    requestProductInformationList.get(3).countryObject = countryObject; 
                if(ApexPages.currentPage().getParameters().get('productFiveCountry') == countryObject.Id)
                    requestProductInformationList.get(4).countryObject = countryObject; 
            }
        }
        
        if(contactMap.size() > 0)
        {
            for(Contact contactObject : [SELECT Id, Name, Arabic_Name__c, Passport_Number_Formula__c, Occupation__c, Job_Title_Arabic__c, Nationality__r.Name, Nationality__r.Country_Arabic_Name__c, (SELECT Monthly_Salary__c FROM Employee_Contracts__r) FROM Contact WHERE Id IN: contactMap.keyset()])
            {
                if(ApexPages.currentPage().getParameters().get('contact') == contactObject.Id)
                    currentEmployee = contactObject;    
                if(ApexPages.currentPage().getParameters().get('permitEmployeeOne') == contactObject.Id)
                    entryPermitsEmployeesList.add(contactObject);    
                else if(ApexPages.currentPage().getParameters().get('permitEmployeeTwo') == contactObject.Id)
                    entryPermitsEmployeesList.add(contactObject);    
                else if(ApexPages.currentPage().getParameters().get('permitEmployeeThree') == contactObject.Id)
                    entryPermitsEmployeesList.add(contactObject);  
                else if(ApexPages.currentPage().getParameters().get('permitEmployeeFour') == contactObject.Id)
                    entryPermitsEmployeesList.add(contactObject);
                else if(ApexPages.currentPage().getParameters().get('permitEmployeeFive') == contactObject.Id)
                    entryPermitsEmployeesList.add(contactObject);
            
            
            }
        }
        
        if(requestedForMap.size() > 0)
        {
        
            for(Requested_For__c requestForObject : [SELECT Id, Name FROM Requested_For__c WHERE Id IN: requestedForMap.keyset()] )
            {
                if(ApexPages.currentPage().getParameters().get('requestedFor') == requestForObject.Id)
                    currentRequestedFor = requestForObject;    
                if(ApexPages.currentPage().getParameters().get('oldLicense') == requestForObject.Id)
                    currentOldDrivingLicenseIssued = requestForObject;    
                if(ApexPages.currentPage().getParameters().get('newLicense') == requestForObject.Id)
                    currentNewDrivingLicenseIssued = requestForObject;    
                if(ApexPages.currentPage().getParameters().get('emirateId') == requestForObject.Id)
                    currentEmirate = requestForObject;    
                if(ApexPages.currentPage().getParameters().get('vehicleTypeId') == requestForObject.Id)
                    currentVehicleType = requestForObject; 
                if(ApexPages.currentPage().getParameters().get('portName') == requestForObject.Id)
                    currentPort = requestForObject;
                if(ApexPages.currentPage().getParameters().get('requestType') == requestForObject.Id)
                    currentRequestType = requestForObject; 
                if(ApexPages.currentPage().getParameters().get('purposeVisit') == requestForObject.Id)
                    currentPurposeOfVisit = requestForObject;  
                if(ApexPages.currentPage().getParameters().get('addressToId') == requestForObject.Id)
                    currentAddressTo = requestForObject;
                if(ApexPages.currentPage().getParameters().get('productOne') == requestForObject.Id)
                    requestProductInformationList.get(0).requestedForTypeObject = requestForObject;
                if(ApexPages.currentPage().getParameters().get('productTwo') == requestForObject.Id)
                    requestProductInformationList.get(1).requestedForTypeObject = requestForObject;
                if(ApexPages.currentPage().getParameters().get('productThree') == requestForObject.Id)
                    requestProductInformationList.get(2).requestedForTypeObject = requestForObject;
                if(ApexPages.currentPage().getParameters().get('productFour') == requestForObject.Id)
                    requestProductInformationList.get(3).requestedForTypeObject = requestForObject;
                if(ApexPages.currentPage().getParameters().get('productFive') == requestForObject.Id)
                    requestProductInformationList.get(4).requestedForTypeObject = requestForObject;
                    
            
            }
        }
        
        if(ApexPages.currentPage().getParameters().get('productOneInvoice') != null)
            requestProductInformationList.get(0).InvoiceNumber = ApexPages.currentPage().getParameters().get('productOneInvoice');
        if(ApexPages.currentPage().getParameters().get('productTwoInvoice') != null)
            requestProductInformationList.get(1).InvoiceNumber = ApexPages.currentPage().getParameters().get('productTwoInvoice');
        if(ApexPages.currentPage().getParameters().get('productThreeInvoice') != null)
            requestProductInformationList.get(2).InvoiceNumber = ApexPages.currentPage().getParameters().get('productThreeInvoice');
        if(ApexPages.currentPage().getParameters().get('productFourInvoice') != null)
            requestProductInformationList.get(3).InvoiceNumber = ApexPages.currentPage().getParameters().get('productFourInvoice');
        if(ApexPages.currentPage().getParameters().get('productFiveInvoice') != null)
            requestProductInformationList.get(4).InvoiceNumber = ApexPages.currentPage().getParameters().get('productFiveInvoice');
        
        if(ApexPages.currentPage().getParameters().get('productOneShipment') != null)
            requestProductInformationList.get(0).ShipmentNumber = ApexPages.currentPage().getParameters().get('productOneShipment');
        if(ApexPages.currentPage().getParameters().get('productTwoShipment') != null)
            requestProductInformationList.get(1).ShipmentNumber = ApexPages.currentPage().getParameters().get('productTwoShipment');
        if(ApexPages.currentPage().getParameters().get('productThreeShipment') != null)
            requestProductInformationList.get(2).ShipmentNumber = ApexPages.currentPage().getParameters().get('productThreeShipment');
        if(ApexPages.currentPage().getParameters().get('productFourShipment') != null)
            requestProductInformationList.get(3).ShipmentNumber = ApexPages.currentPage().getParameters().get('productFourShipment');
        if(ApexPages.currentPage().getParameters().get('productFiveShipment') != null)
            requestProductInformationList.get(4).ShipmentNumber = ApexPages.currentPage().getParameters().get('productFiveShipment');
        
        if(ApexPages.currentPage().getParameters().get('productOneDate') != null)
            requestProductInformationList.get(0).productDate = Date.valueOf(ApexPages.currentPage().getParameters().get('productOneDate'));
        if(ApexPages.currentPage().getParameters().get('productTwoDate') != null)
            requestProductInformationList.get(1).productDate = Date.valueOf(ApexPages.currentPage().getParameters().get('productTwoDate'));
        if(ApexPages.currentPage().getParameters().get('productThreeDate') != null)
            requestProductInformationList.get(2).productDate = Date.valueOf(ApexPages.currentPage().getParameters().get('productThreeDate'));
        if(ApexPages.currentPage().getParameters().get('productFourDate') != null)
            requestProductInformationList.get(3).productDate = Date.valueOf(ApexPages.currentPage().getParameters().get('productFourDate'));
        if(ApexPages.currentPage().getParameters().get('productFiveDate') != null)
            requestProductInformationList.get(4).productDate = Date.valueOf(ApexPages.currentPage().getParameters().get('productFiveDate'));
        
        //Now Map with the current records....
        if(recordTypeMap.get(recordTypeName) != null)
            currentRecord.RecordTypeId = recordTypeMap.get(recordTypeName);
        currentRecord.Transaction_Type__c = currentTransactionType.Id;
        currentRecord.Company__c = currentAccount.Id;
        //currentRecord.Bank_Name__c = bankName;
        //currentRecord.Etisalat_Service__c = etisalatService;
        currentRecord.Requested_For__c = currentRequestedFor.Id;
        currentRecord.Delivery_Method__c = ApexPages.currentPage().getParameters().get('delivery');
        currentRecord.Old_Location_Name__c = ApexPages.currentPage().getParameters().get('oldLocation');
        currentRecord.New_Location_Name__c = newLocation.Id;
        currentRecord.Telephone_Number__c = ApexPages.currentPage().getParameters().get('telephone');
        currentRecord.Visiting_Country__c = currentVisitingCountry.Id;
        currentRecord.Purpose_Of_Visit__c = currentPurposeOfVisit.Id;
        currentRecord.Address_To_Lookup__c = currentAddressTo.Id;
        currentRecord.Contact__c = currentEmployee.Id;
        currentRecord.Previous_Company_Name__c = ApexPages.currentPage().getParameters().get('previousCompanyName');
        currentRecord.Number_Of_Entry_Permits__c = ApexPages.currentPage().getParameters().get('numberOfPermits');
        currentRecord.Port_Name__c = currentPort.Id;
        currentRecord.Request_Type__c = currentRequestType.Id;
        currentRecord.Custom_Code_Number__c = ApexPages.currentPage().getParameters().get('customCodeNumber');
        currentRecord.Old_Driving_License_Country__c = currentOldDrivingLicenseCountry.Id;
        currentRecord.New_Driving_License_Country__c = currentNewDrivingLicenseCountry.Id;
        currentRecord.Old_Driving_License_Issued_Emirates__c = currentOldDrivingLicenseIssued.Id;
        currentRecord.New_Driving_License_Issued_Emirates__c = currentNewDrivingLicenseIssued.Id;
        currentRecord.License_Plate_Number_Code__c = ApexPages.currentPage().getParameters().get('plateNumber');
        currentRecord.Code__c = ApexPages.currentPage().getParameters().get('code');
        currentRecord.Emirates__c = currentEmirate.Id;
        currentRecord.Vehicle_Type_Lookup__c = currentVehicleType.Id;
        currentRecord.Vehicle_Model__c = ApexPages.currentPage().getParameters().get('vehicleModel');
        currentRecord.Number_Of_Entry_Permits__c = ApexPages.currentPage().getParameters().get('numberOfProducts');
        
        if(ApexPages.currentPage().getParameters().get('delivery')== 'Courier')
        {
            currentRecord.Courier_Delivery_Address__c = ApexPages.currentPage().getParameters().get('address');
            currentRecord.Courier_Delivery_City__c = ApexPages.currentPage().getParameters().get('city');
            currentRecord.Courier_Delivery_Country__c = ApexPages.currentPage().getParameters().get('country');
            currentRecord.Courier_Delivery_Country_Lookup__c = currentCourierCountry.Id;
            currentRecord.Courier_Delivery_Contact_Mobile__c = ApexPages.currentPage().getParameters().get('mobile');
        }
        
        //This should stay hardcoded values.
        currentRecord.Submission_Channel__c = 'Mobile App';
        currentRecord.Request_Status__c = 'Under Process';
           
        //Comment by Mustafa Zaidi... [17th April 2014]
        /*String transactionTypeId = ApexPages.currentPage().getParameters().get('transactionTypeId');
        String requestedFor = ApexPages.currentPage().getParameters().get('requestedFor');
        //String bankName = ApexPages.currentPage().getParameters().get('bank');
        //String etisalatService = ApexPages.currentPage().getParameters().get('etisalatService');
        String deliveryMethod = ApexPages.currentPage().getParameters().get('delivery');
        String courierAddress = ApexPages.currentPage().getParameters().get('address');
        String courierCity = ApexPages.currentPage().getParameters().get('city');
        String courierCountry = ApexPages.currentPage().getParameters().get('country');
        String courierCountryLookUp = ApexPages.currentPage().getParameters().get('countryId');
        String courierMobile = ApexPages.currentPage().getParameters().get('mobile');
        String oldLocationName = ApexPages.currentPage().getParameters().get('oldLocation');
        String newLocationId = ApexPages.currentPage().getParameters().get('newLocation');
        String telephoneNumber = ApexPages.currentPage().getParameters().get('telephone');
        String visitingCountryId = ApexPages.currentPage().getParameters().get('visitingCountry');
        String purposeOfVisit = ApexPages.currentPage().getParameters().get('purposeVisit');
        String contactId = ApexPages.currentPage().getParameters().get('contact');
        String previousCompanyName = ApexPages.currentPage().getParameters().get('previousCompanyName');
        String numberOfEntryPermits = ApexPages.currentPage().getParameters().get('numberOfPermits');
        String permitEmployeeOne = ApexPages.currentPage().getParameters().get('permitEmployeeOne');
        String permitEmployeeTwo = ApexPages.currentPage().getParameters().get('permitEmployeeTwo');
        String permitEmployeeThree = ApexPages.currentPage().getParameters().get('permitEmployeeThree');
        String permitEmployeeFour = ApexPages.currentPage().getParameters().get('permitEmployeeFour');
        String permitEmployeeFive = ApexPages.currentPage().getParameters().get('permitEmployeeFive');
        String portId = ApexPages.currentPage().getParameters().get('portName');
        String requestTypeId = ApexPages.currentPage().getParameters().get('requestType');
        String customCodeNumber = ApexPages.currentPage().getParameters().get('customCodeNumber');
        String occupationId = ApexPages.currentPage().getParameters().get('previousJobId');
        String oldDrivingLicenseCountryId = ApexPages.currentPage().getParameters().get('oldCountry');
        String newDrivingLicenseCountryId = ApexPages.currentPage().getParameters().get('newCountry');
        string oldDrivingLicenseIssuesId = ApexPages.currentPage().getParameters().get('oldLicense');
        string newDrivingLicenseIssuesId = ApexPages.currentPage().getParameters().get('newLicense');
        string emirateId = ApexPages.currentPage().getParameters().get('emirateId');
        string code = ApexPages.currentPage().getParameters().get('code');
        string plateNumber = ApexPages.currentPage().getParameters().get('plateNumber');
        string vehicleTypeId = ApexPages.currentPage().getParameters().get('vehicleTypeId');
        string vehicleModel = ApexPages.currentPage().getParameters().get('vehicleModel');
        
        //Added by Mustafa Zaidi [17th April 2014]
        currentEmployee = new Contact();
                
        currentTransactionType = [SELECT Id, Name, Application_Package__c, Application_Type__c, Transaction_Description__c FROM Transaction_Types__c WHERE Id = :transactionTypeId];
        
        if(vehicleTypeId != null)
            currentVehicleType = [SELECT Id, Name FROM Requested_For__c WHERE Id = :vehicleTypeId];
        
        if(emirateId != null)
            currentEmirate = [SELECT Id, Name FROM Requested_For__c WHERE Id = :emirateId];
        
        if(oldDrivingLicenseIssuesId != null)
            currentOldDrivingLicenseIssued = [SELECT Id, Name FROM Requested_For__c WHERE Id = :oldDrivingLicenseIssuesId];
        
        if(newDrivingLicenseIssuesId != null)
            currentNewDrivingLicenseIssued = [SELECT Id, Name FROM Requested_For__c WHERE Id = :newDrivingLicenseIssuesId];
        
        if(oldDrivingLicenseCountryId != null)
            currentOldDrivingLicenseCountry = [SELECT Id, Name FROM Country__c WHERE Id = :oldDrivingLicenseCountryId];
        
        if(newDrivingLicenseCountryId != null)
            currentNewDrivingLicenseCountry = [SELECT Id, Name FROM Country__c WHERE Id = :newDrivingLicenseCountryId];
        
        if(occupationId != null)
            previousJobTitle = [SELECT Id, Name FROM Occupation__c WHERE Id = :occupationId];
        
        if(requestTypeId != null)
            currentRequestType = [SELECT Id, Name FROM Requested_For__c WHERE Id = :requestTypeId];
        
        if(permitEmployeeOne != null)
            entryPermitsEmployeesList = [SELECT Id, Name, Arabic_Name__c, Passport_Number_Formula__c, Occupation__c,
                                    Job_Title_Arabic__c, Nationality__r.Name, Nationality__r.Country_Arabic_Name__c
                                    FROM Contact 
                                    WHERE Id IN (:permitEmployeeOne, :permitEmployeeTwo, :permitEmployeeThree,
                                                 :permitEmployeeFour, :permitEmployeeFive)];
        
        if(visitingCountryId != null)
            currentVisitingCountry = [SELECT Id, Name FROM Country__c WHERE Id = :visitingCountryId];
        
        if(courierCountryLookUp != null)
            currentCourierCountry = [SELECT Id, Name FROM Country__c WHERE Id = :courierCountryLookUp];
        
        if(purposeOfVisit != null)
            currentPurposeOfVisit = [SELECT Id, Name FROM Requested_For__c WHERE Id = :purposeOfVisit];
            
        if(portId != null)
            currentPort = [SELECT Id, Name FROM Requested_For__c WHERE Id = :portId];
        
        if(contactId != null)
            currentEmployee = [SELECT Id, Name, Arabic_Name__c, Passport_Number_Formula__c, Occupation__c,
                                Job_Title_Arabic__c, Nationality__r.Name, Nationality__r.Country_Arabic_Name__c
                                FROM Contact 
                                WHERE Id = :contactId];
        
        if(requestedFor != null)
            currentRequestedFor = [SELECT Id, Name FROM Requested_For__c WHERE Id = :requestedFor];
        
        if(newLocationId != null)
            newLocation = [SELECT Id, Name FROM Location__c WHERE Id = :newLocationId];
        
        String Uid = UserInfo.getUserId();
        List<Account> accountsList = [SELECT Id, Name, LicenseNumber__r.Name, Company_Name_Arabic__c,
                        Authorized_Manager__r.Name, Authorized_Manager__r.Arabic_Name__c,
                        Authorize_Manager_Nationality__c, Authorize_Manager_Passport_Number__c
                        FROM Account WHERE Id IN (SELECT AccountId FROM User WHERE Id = :Uid) LIMIT 1];
        
        
        if( accountsList.size() > 0)
            currentAccount = accountsList[0];
        else
            currentAccount = new Account();
        */
        
        
        
        
        /*currentRecord = (Request__c)controller.getRecord();
        //currentRecord.RecordTypeId = [Select Id From RecordType Where SObjectType ='Request__c' And DeveloperName = 'Request_Service_Letter_Bank_Letter' Limit 1][0].Id;
        
        if(recordTypeMap.get(recordTypeName) != null)
            currentRecord.RecordTypeId = recordTypeMap.get(recordTypeName);
        currentRecord.Transaction_Type__c = transactionTypeId;
        currentRecord.Company__c = currentAccount.Id;
        //currentRecord.Bank_Name__c = bankName;
        //currentRecord.Etisalat_Service__c = etisalatService;
        currentRecord.Requested_For__c = requestedFor;
        currentRecord.Delivery_Method__c = deliveryMethod;
        currentRecord.Old_Location_Name__c = oldLocationName;
        currentRecord.New_Location_Name__c = newLocationId;
        currentRecord.Telephone_Number__c = telephoneNumber;
        currentRecord.Visiting_Country__c = visitingCountryId;
        currentRecord.Purpose_Of_Visit__c = purposeOfVisit;
        currentRecord.Contact__c = contactId;
        currentRecord.Previous_Company_Name__c = previousCompanyName;
        currentRecord.Number_Of_Entry_Permits__c = numberOfEntryPermits;
        currentRecord.Port_Name__c = portId;
        currentRecord.Request_Type__c = requestTypeId;
        currentRecord.Custom_Code_Number__c = customCodeNumber;
        currentRecord.Old_Driving_License_Country__c = oldDrivingLicenseCountryId;
        currentRecord.New_Driving_License_Country__c = newDrivingLicenseCountryId;
        currentRecord.Old_Driving_License_Issued_Emirates__c = oldDrivingLicenseIssuesId;
        currentRecord.New_Driving_License_Issued_Emirates__c = newDrivingLicenseIssuesId;
        currentRecord.License_Plate_Number_Code__c = plateNumber;
        currentRecord.Code__c = code;
        currentRecord.Emirates__c = emirateId;
        currentRecord.Vehicle_Type_Lookup__c = vehicleTypeId;
        currentRecord.Vehicle_Model__c = vehicleModel;
        
        if(deliveryMethod == 'Courier')
        {
            currentRecord.Courier_Delivery_Address__c = courierAddress;
            currentRecord.Courier_Delivery_City__c = courierCity;
            currentRecord.Courier_Delivery_Country__c = courierCountry;
            currentRecord.Courier_Delivery_Country_Lookup__c = courierCountryLookUp;
            currentRecord.Courier_Delivery_Contact_Mobile__c = courierMobile;
        }
        
        //This should stay hardcoded values.
        currentRecord.Submission_Channel__c = 'Mobile App';
        currentRecord.Request_Status__c = 'Under Process';
        */
        
    }
    /*
    public Country__c getCurrentCourierCountry()
    {
        return currentCourierCountry;
    }
    public Requested_For__c getCurrentVehicleType()
    {
        return currentVehicleType;
    }
    
    public Requested_For__c getCurrentEmirate()
    {
        return currentEmirate;
    }
    
    public Requested_For__c getCurrentOldDrivingLicenseIssued()
    {
        return currentOldDrivingLicenseIssued;
    }
    
    public Requested_For__c getCurrentNewDrivingLicenseIssued()
    {
        return currentNewDrivingLicenseIssued;
    }
    
    public Country__c getCurrentOldDrivingLicenseCountry()
    {
        return currentOldDrivingLicenseCountry;
    }
    
    public Country__c getCurrentNewDrivingLicenseCountry()
    {
        return currentNewDrivingLicenseCountry;
    }
    
    public Occupation__c getPreviousJobTitle()
    {
        return previousJobTitle;
    }
    
    public Account getCurrentAccount()
    {
        return currentAccount;
    }
    public Requested_For__c getCurrentRequestedFor()
    {
        return currentRequestedFor;
    }
    
    
    public Transaction_Types__c getCurrentTransactionType()
    {
        return currentTransactionType;
    }
    
    public Location__c getNewLocation()
    {
        return newLocation;
    }
    
    public Requested_For__c getCurrentRequestType()
    {
        return currentRequestType;
    }
    
    
    
    public Country__c getCurrentVisitingCountry()
    {
        return currentVisitingCountry;
    }
    public String getCourierCountry()
    {
        return currentRecord.Courier_Delivery_Country__c;
    }
    
    
    public Requested_For__c getCurrentPurposeOfVisit()
    {
        return currentPurposeOfVisit;
    }
    
    public Requested_For__c getCurrentPort()
    {
        return currentPort;
    }
    
    public Contact getCurrentEmployee()
    {
        return currentEmployee;
    }    
    public List<Contact> getEntryPermitsEmployeesList()
    {
        return entryPermitsEmployeesList;
    }
    */
    public PageReference saveAndThankYou()
    {
        try
        {
        
            //Insert Record into salesforce
            insert currentRecord;
            
            if(requestProductInformationList != null && requestProductInformationList.size() > 0)
            {
                List<Product_Information__c> newProductInformationList = new List<Product_Information__c>();
                for (RequestProductInformation product : requestProductInformationList)
                {
                    Product_Information__c productInformation = new Product_Information__c();
                    productInformation.Request_Number__c  = currentRecord.Id;
                    productInformation.Invoice_Number__c = product.InvoiceNumber;
                    productInformation.Shipment_Number__c = product.ShipmentNumber;
                    productInformation.Date__c = product.productDate;
                    productInformation.Country_Of_Origin__c = product.countryObject.Id;
                    productInformation.Product_Type__c = product.requestedForTypeObject.Id;
                    productInformation.Company__c = currentAccount.Id;
                    
                    newProductInformationList.add(productInformation);
                }
                
                insert newProductInformationList;
            }
            
            if(entryPermitsEmployeesList != null)
            {
                List<Entry_Permit__c> newEntryPermitList = new List<Entry_Permit__c>();
                for (Contact permitEmployee : entryPermitsEmployeesList)
                {
                    Entry_Permit__c entryPermit = new Entry_Permit__c();
                    entryPermit.Company__c = currentAccount.Id;
                    entryPermit.Employee__c = permitEmployee.Id;
                    entryPermit.Request_Number__c = currentRecord.Id;
                    
                    newEntryPermitList.add(entryPermit);
                }
                
                insert newEntryPermitList;
            }
            
            if (currentTransactionType.Application_Package__c == 'Service Letter' &&
                currentTransactionType.Application_Type__c == 'Certificate' &&
                currentTransactionType.Transaction_Description__c == 'Certificate of Incorporation')
            {
                List<Account_Activity__c> accountActivityList = new List <Account_Activity__c>
                                                                        ([SELECT Id, Account__c, Activity__c 
                                                                          FROM Account_Activity__c
                                                                          WHERE Active__c = true]);
                if(accountActivityList.size() > 0)
                {
                    List<Account_Activity_Request__c> newAccountActivityRequestList = new List<Account_Activity_Request__c> ();
                    
                    for(Account_Activity__c accountActivityItem : accountActivityList)
                    {
                        Account_Activity_Request__c tempAccountActivityRequest = new Account_Activity_Request__c();
                        tempAccountActivityRequest.Request_Number__c = currentRecord.Id;
                        tempAccountActivityRequest.Company__c = accountActivityItem.Account__c;
                        tempAccountActivityRequest.Activity__c = accountActivityItem.Activity__c;
                        
                        newAccountActivityRequestList.add(tempAccountActivityRequest);
                    }
                    
                    insert newAccountActivityRequestList;
                }
            }
            
            
            //PO removed - 7/23/2014 - moved as trigger
            /*
            if (currentTransactionType.Application_Package__c == 'Service Letter' &&
                ((currentTransactionType.Application_Type__c == 'Certificate' &&
                (currentTransactionType.Transaction_Description__c == 'Share Certificate' || 
                currentTransactionType.Transaction_Description__c == 'Certificate of Good Standing' ||
                currentTransactionType.Transaction_Description__c == 'Certificate of Incorporation')) ||
                (currentTransactionType.Application_Type__c == 'Bank Letter' &&
                currentTransactionType.Transaction_Description__c == 'Open Bank Account')))
                
                
            {
                List<Share_Ownership__c> shareOwnershipList = [SELECT Id, Company__c, Shareholder__c, Share_Profit__c
                                                                FROM Share_Ownership__c
                                                                WHERE Company__c = :currentAccount.Id];
                                                                
                if (shareOwnershipList != null)
                {
                    List<Share_Ownership_Request__c> ownershipRequestList = new List<Share_Ownership_Request__c>();
                    for (Share_Ownership__c ownership : shareOwnershipList)
                    {
                        Share_Ownership_Request__c ownershipRequest = new Share_Ownership_Request__c();
                        ownershipRequest.Request_Number__c = currentRecord.Id;
                        ownershipRequest.Company__c = ownership.Company__c;
                        ownershipRequest.Shareholder__c = ownership.Shareholder__c;
                        ownershipRequest.Share__c = ownership.Share_Profit__c;
                        
                        ownershipRequestList.add(ownershipRequest);
                    }
                    
                    insert ownershipRequestList;
                }
            }
            */
            
            //To get the auto-number field....
            Request__c insertedRecord = [SELECT Id, Name FROM Request__c WHERE Id = :currentRecord.Id];
            
            //Redirect to Thank You page with Service Letter Number
            PageReference pageRef = new PageReference('/apex/MobileAppThankYou?letterName=' + insertedRecord.Name);
            pageRef.setRedirect(true);
            return pageRef;   
        }
        catch(Exception exp)
        {
            System.debug('Exception => '+ exp.getMessage() + ' : Line Number => '+ exp.getLineNumber());
            return null;
        }
    }
}